// components/information-picker/information-picker.ts
Component({

  /**
   * 组件的属性列表
   */
  lifetimes: {
    attached: function () {
      const valueIndex = this.properties.group.findIndex((item) => item.name === this.properties.value)

      this.setData({
        pickerValue: [valueIndex],
        id: this.properties.group[valueIndex].id,
        name: this.properties.group[valueIndex].name,
      })
    }
  },
  properties: {
    label: {
      type: String,
      value: "",
    },
    value: {
      type: String,
      value: "",
    },
    group: {
      type: Array,
      value: []
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    pickerValue: [] as Number[],
    name: "",
    id: "",
  },

  /**
   * 组件的方法列表
   */
  methods: {
    handlePicker(e: any) {
      const val = e.detail.value
      this.setData({
        name: this.properties.group[val].name,
        id: this.properties.group[val].id
      })
    },
    handleCancle() {
      this.triggerEvent("pickerCancle")
    },
    handleConfirm() {
      this.triggerEvent("pickerConfirm", {
        id: this.data.id,
        name: this.data.name
      })
    }
  }
})